package com.qianyu.project.core.mapper.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author gs
 * @date create in 2020/12/11 16:28
 */
public class FormProvider {

    public String save(){
        return new SQL(){
            {
                INSERT_INTO("form");
                INTO_VALUES("null","#{eid}","#{createdate}","#{flowId}",
                        "#{current}","#{status}","#{id}","#{target}","1");
            }
        }.toString();
    }

    public String selectByRole(){
        return new SQL(){
            {
                SELECT("f.*,fw.flowName");
                FROM("form f,flow_node fn,system_role r,flow fw");
                WHERE("f.current=fn.flowNodeId and fn.flowNodeRole=r.id");
                WHERE("fw.flowId=f.flowId");
                WHERE("r.id in(${value}) and f.isvalid=1");
            }
        }.toString();
    }

    public String selectByUser(){
        return new SQL(){
            {
                SELECT("f.*,fw.flowName");
                FROM("form f,system_user u,flow fw");
                WHERE("u.id=f.eid and f.flowId=fw.flowId");
                WHERE("u.id=#{value} and f.isvalid=1");
            }
        }.toString();
    }


    public String selectMybacklog(IPage page, Map<String,String> params){
        return new SQL(){
            {
                  SELECT("f.*,fl.flowName,su.user_realname");
                  FROM("form f ,flow_node fn,flow fl,system_user su");
                  WHERE("f.current=fn.flowNodeId and f.isvalid=1 and fl.flowId=f.flowId and su.id=f.eid and su.user_status='Y' and fl.isvalid=1 and fn.isvalid=1 and fn.flowNodeRole in (select sur.system_role_id id from system_user su,system_user_role sur where su.id=sur.system_user_id and sur.status='Y' and su.id=#{params.id}) ");
                    if(!StringUtils.isEmpty(params.get("flowName"))){
                        WHERE("fl.flowName like '%"+params.get("flowName")+"%' ");
                    }
                    if(!StringUtils.isEmpty(params.get("createdate")) && StringUtils.isEmpty(params.get("createdate1"))){
                        WHERE("f.createdate = '"+params.get("createdate")+"'");
                    }
                    if(!StringUtils.isEmpty(params.get("createdate1")) && StringUtils.isEmpty(params.get("createdate")) ){
                        WHERE("f.createdate = '"+params.get("createdate1")+"'");
                    }
                    if(!StringUtils.isEmpty(params.get("createdate")) && !StringUtils.isEmpty(params.get("createdate1")) ){
                        WHERE(" f.createdate between  '"+params.get("createdate")+"'  and  '"+params.get("createdate1")+"'");
                    }
            }
        }.toString();
    }

}
